package com.ruelala;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;

import org.archive.crawler.datamodel.CrawlURI;
import org.archive.net.UURI;
import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.HasParentFilter;
import org.htmlparser.filters.NodeClassFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.tags.ImageTag;
import org.htmlparser.tags.InputTag;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;

import com.dao.DBUtil;
import com.dao.DealBean;
import com.mongodb.MongoException;

public class RuelalaParser {
	
	public  void parserToDB(CrawlURI curi)  {  
        
		DealBean dealbean = new DealBean();

		try {  
			String skuId = "";
			
            Parser parser = new Parser(curi.toString()); 
            
            parser.setEncoding("UTF-8"); 
            
            UURI uuri = curi.getUURI();
            
            dealbean.setBuy_url(uuri.toString());
            
            String category1 = curi.getCategory1();
            String category2 = curi.getCategory2();
            
            dealbean.setCategory1(category1);
            dealbean.setCategory2(category2);
            
            NodeFilter skuIdNodeFilter = new AndFilter(new TagNameFilter("input"),new HasAttributeFilter("name", "PRODUCT&lt;&gt;prd_id"));
            
            NodeList skuIdNodeList = parser.extractAllNodesThatMatch(skuIdNodeFilter); 
            
            if (skuIdNodeList != null && skuIdNodeList.size() > 0) {  

                Node skuIdNode = skuIdNodeList.elementAt(0);  

                skuId = ((InputTag)skuIdNode).getAttribute("value");  

                dealbean.setSku_id(skuId);

            }            
            parser.reset();
            
            
            NodeFilter brandNodeFilter = new AndFilter(new TagNameFilter("h1"),new HasAttributeFilter("class", "boldBlackText12"));
            
            NodeList brandNodeList = parser.extractAllNodesThatMatch(brandNodeFilter); 
            
            if (brandNodeList != null && brandNodeList.size() > 0) {  

                Node brandNode = brandNodeList.elementAt(0);  

               String brand = brandNode.toPlainTextString().trim();

                dealbean.setBrand(brand);

            }            
            parser.reset();
            
            
            NodeFilter product_nameNodeFilter = new AndFilter(new TagNameFilter("h2"),new HasAttributeFilter("class", "boldBlackText12"));
            
            NodeList product_nameNodeList = parser.extractAllNodesThatMatch(product_nameNodeFilter); 
            
            if (product_nameNodeList != null && product_nameNodeList.size() > 0) {  

                Node product_nameNode = product_nameNodeList.elementAt(0);  

               String product_name = product_nameNode.toPlainTextString().trim();

                dealbean.setProduct_name(product_name);

            }            
            parser.reset();
            
     
            NodeFilter original_priceNodeFilter = new AndFilter(new TagNameFilter("span"),new HasAttributeFilter("class", "product-price"));
            
            NodeList original_priceNodeList = parser.extractAllNodesThatMatch(original_priceNodeFilter); 
            
            if (original_priceNodeList != null && original_priceNodeList.size() > 0) {  

                Node original_priceNode = original_priceNodeList.elementAt(0);  

               String original_price = original_priceNode.toPlainTextString().trim();
               
                //char cr[]=original_price.trim().toCharArray();
                
                dealbean.setOriginal_price(original_price);

            }            
            parser.reset();
            
            NodeFilter sale_priceNodeFilter = new AndFilter(new TagNameFilter("span"),new HasAttributeFilter("class", "product-sale-price"));
            
            NodeList sale_priceNodeList = parser.extractAllNodesThatMatch(sale_priceNodeFilter); 
            
            if (sale_priceNodeList != null && sale_priceNodeList.size() > 0) {  

                Node sale_priceNode = sale_priceNodeList.elementAt(0);  

               String sale_price = sale_priceNode.toPlainTextString().trim();

                dealbean.setSale_price(sale_price);

            }            
            parser.reset();

            
            NodeFilter product_descriptionNodeFilter = new AndFilter(new TagNameFilter("span"),new HasAttributeFilter("id", "api_prod_copy1"));
            
            NodeList product_descriptionNodeList = parser.extractAllNodesThatMatch(product_descriptionNodeFilter); 
            
            if (product_descriptionNodeList != null && product_descriptionNodeList.size() > 0) {  

                Node product_descriptionNode = product_descriptionNodeList.elementAt(0);  

               String product_description = product_descriptionNode.toPlainTextString().trim();

                dealbean.setProduct_description(product_description);

            }            
            parser.reset();
            
            
            dealbean.setBuy_url(curi.toString());
            dealbean.setSouce_site("saksfifthavenue");
          }catch(ParserException e){  
//
            e.printStackTrace();  
//
          }  
//          
//          
//          if(giltBean.getSku_id()!=null){
          
          DBUtil.save(dealbean);
         // }

  }  
	
	
	
	
    public static void main(String []args)
    {
    	RuelalaParser sp=new RuelalaParser();
    	sp.parserToDB(null);
    }

 
}
